<!--
	Created by qiao on 2018/6/5.
-->
<template>
    <div class="Promotion-Item">
        <div class="top">
            <div class="left">
                <h1>We need some information</h1>
                <h3>Please fill in your shipping and billing information before fin</h3>
            </div>
            <div class="right green" @click="returnCart"><i>< </i>Return To Cart</div>
        </div>
        <div class="main">
            <div class="left">
                <div class="paypal">
                    <h2 class="title">Payment Method</h2>
                    <div class="checkBox">
                        <span class="quan" :class="{'checked' : checked}"></span>
                        <img src="../../../static/img/paypal.png"/>
                        <!--当前只有 paypal一种支付方式 禁止取消钩选-->
                        <input type="checkbox" onclick="return false;" checked="checked"/>
                    </div>
                </div>
                <div class="produectBox">
                    <h2 class="title clearfix">
                      <span class="title-left">Your Items</span>
                      <div class="time-box" v-if="orderData.lastModifiedTime">
                        <time-down @time-end="timeEnd" :endTime="orderData.lastModifiedTime" :upTime="true"></time-down>
                      </div>
                    </h2>
                    <ul>
                        <li v-for="(item,index) in orderData.orderItems" :key="index">
                            <div class="left" >
                              <img  v-lazy="imgUrl + item.productInOrder.images[0].src + '?x-oss-process=image/resize,h_500,limit_0'">
                            </div>
                            <div class="right">
                                <div class="produectData">
                                    <div class="left">
                                        <h4 :title="item.productInOrder.headLine">
                                          {{item.productInOrder.headLine}}
                                        </h4>
                                        <h5 v-show="item.productInOrder.variation">{{skuFn(item.productInOrder.variation)}}</h5>
                                        <h2>Qty:<span>{{item.qty}}</span></h2>
                                    </div>
                                    <div class="right">
                                        <span>${{item.productInOrder.price}}</span>
                                        <p><s>${{item.productInOrder.originalPrice}}</s><span class="blue" style="font-size:14px;padding-right: 2px">You Save ${{saveFn(item.productInOrder.originalPrice,item.productInOrder.price)}}</span></p>
                                    </div>
                                </div>
                                <div class="freeShip">Free shipping</div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="floal">
                <div class="box" id="box" :style="'top:' + scrollTop + 'px'">
                    <div class="title">Cart Sumary</div>
                    <div class="subto">
                        <div>
                            <span>Subtotal(({{orderData.orderItems ? orderData.orderItems.length : '0'}} items)</span>
                            <span>${{amount}}</span>
                        </div>
                        <div>
                            <span>Shipping & Handling</span>
                            <span class="green">Free</span>
                        </div>
                    </div>
                    <div class="total">
                        <div class="price">
                            <span class="bold">Order Total:</span>
                            <div>
                                <span class="bold">${{amount}}</span>
                            </div>
                          <span class="saveMoney">You Save ${{saveMoney}}</span>
                        </div>
                        <h4>By clicking below, I agree to <span class="green" @click="goAboutUs">The Aboout Us </span>,<span class="green" @click="goPrivacyPolicy"> Privacy Policy</span> & <span class="green" @click="Conditions">Terms and Conditions</span></h4>
                      <div  class="input">Place Order
                          <div id="paypal-button-container"></div>
                      </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</template>

<script type="es6">
  import {mapMutations,mapActions} from 'vuex'
  import {getRequestDetail} from '../../api/requests';
  import { customerPaid } from '../../api/my-order';
  import TimeDown from 'base/countdown/countdown';
  import monitoring from '../../common/js/monitoring';
    export default {
        data() {
            return {
              checked:true,     //支付方式默认勾选
              scrollTop:'0',     //右侧悬浮区域top值
              orderNo:"", //订单号
              orderData:"",
              amount:"",    //订单总价
              saveMoney : 0,//节省的总价
              imgUrl: imgUrl,//图片地址前缀
            }
        },
        watch: {},
        created() {
            this.orderNo = this.$route.params.orderNo | sessionStorage['promotionOrderNo'] | '';   //获取订单号
            this.ordersDataFn(this.orderNo,(data)=>{ //请求订单详情
              this.orderData = data;
              if(data.totalAmount.amount) this.amount = data.totalAmount.amount;
              data.orderItems.forEach((item) =>{
                  this.saveMoney += Number(( item.productInOrder.originalPrice - item.productInOrder.price ).toFixed(2))* item.qty
              })

            })
        },
        mounted() {
            document.title = "Promotion Item"; //设置页面title
            window.addEventListener('scroll', this.handleScroll);  //监听滚动高度
          let that = this;
          paypal.Button.render({
            env: 'production',

            style: {
              label: 'checkout',
              size:  'small',    // small | medium | large | responsive
              shape: 'pill',     // pill | rect
              color: 'gold'      // gold | blue | silver | black
            },
            client: {
              sandbox:    'ASnHZ-3-E88_WEoJ6l69eF1GkFwnbjtkx0lDIpO6mR8-FksTVOHuOKY0yb3Oj7uJSGofVXYas6wtL0jF',
              production: 'AZdX0MrZYKrzW-uhu25_z5AeETRZSroR0v3YzGS1AOD1TpOWpWbQ-GM5G1kiiup_xWBWN7qpKFMhIosO',
              // production: 'Aewl0gGrFMzZmDuK-iXMkOCCk21HkmNj7OgYQfphBFOaulvFEN4LmtThFVtg7fYXXmB9V9ykqvQ2gW1w'   //HK
            },
            payment: function(data, actions) {
              return actions.payment.create({
                payment: {
                  transactions: [
                    {
                      amount : { total: that.amount, currency: 'USD' },
                      custom : that.orderNo
                    }
                  ]
                }
              });
            },
            onAuthorize: function(data, actions) {

              return actions.payment.execute().then(function(item) {
                let data = item.payer.payer_info;
                let params = {
                  "paymentId": item.id,
                  "address": {
                    "street1": data.shipping_address.line1 || "",
                    "street2": data.shipping_address.line2 || "",
                    "street3": data.shipping_address.line2 || "",
                    "city": data.shipping_address.city || "",
                    "province": data.shipping_address.state || "",
                    "country": data.shipping_address.country_code || "",
                    "postcode": data.shipping_address.postal_code || ""
                  },
                  "recipient": {
                    "contactName": data.shipping_address.recipient_name || "",
                    "tel": data.phone || "",
                    "email": data.email || "",
                  }
                };
                that.BtnLoading((timer, commit) => {
                  customerPaid(that.orderNo,params).then((data)=>{   //查订单状态是否扭转
                    // if(data.paymentStatus.toLowerCase() == 'paid' || data.paymentStatus.toLowerCase() == 'pending'){

                    let params = {
                      orderNo :that.orderNo,
                      amount:that.amount,
                    };
                    monitoring.orderPay(params);//todo Facebook Pixel & Google Analytic Code    orderPay

                    sessionStorage.removeItem('promotionOrderNo');//支付成功清除订单号
                    clearInterval(timer);
                    commit('SET_ROUTE_LOADING', false);
                    setTimeout(()=>{   //todo Google Analytic 确保监控请求发送出去 延迟页面跳转
                      that.$router.replace({ path: `/payResult/${that.orderNo}` });
                    },250)

                  })
                });
              });

            },
            onCancel: function(data, actions) {       //取消支付
              // console.log(data)
              let params = {
                orderNo :that.orderNo,
                amount:that.amount,
              };
              monitoring.orderPayStop(params);//todo Facebook Pixel & Google Analytic Code    orderPayStop

            },

            onError: function(err) {    //支付报错
              // console.log(err)
            }
          }, '#paypal-button-container');

        },
        methods: {
          ...mapActions([
            'BtnLoading'
          ]),
          timeEnd(){
            this.$router.push({path:'/home'})
          },
          returnCart(){
            this.$router.push({path:'/my-cart/promotion'})
          },
          goAboutUs(){
            this.$router.push({path:'/aboutUs'})
          },
          goPrivacyPolicy(){
            this.$router.push({path:'/privacyStatement'})
          },
          Conditions(){
            this.$router.push({path:'/termsAndConditions'})
          },
          skuFn(data){  //sku处理
              // console.log(data)
              if(data){
                  let sku = [];
                  for(let i of data.productAttributes){
                    sku.push(i.value);
                  }
                  return sku.join("/")
              }
          },
          saveFn(value1,value2){
              return Number((value1 - value2).toFixed(2));
          },
            handleScroll () {  //实时设置top值
              var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
              if (scrollTop < $(".main")[0].offsetTop) {
                this.scrollTop = 0;
              } else if (scrollTop >= $('.main')[0].offsetTop && scrollTop < $('.main')[0].offsetTop + $('.main')[0].offsetHeight) {
                this.scrollTop = scrollTop - $('.main')[0].offsetTop
              } else if (scrollTop >= $('.main')[0].offsetTop + $('.main')[0].offsetHeight) {
                this.scrollTop = $('.main')[0].offsetTop + $('.main')[0].offsetHeight
              }
            },
            ordersDataFn(orderNo,callBack){
              this.BtnLoading((timer, commit) => {
                getRequestDetail(orderNo).then((res) => {
                  callBack(res)
                  clearInterval(timer)
                  commit('SET_ROUTE_LOADING', false)
                }).catch(err => {

                  clearInterval(timer)
                  commit('SET_ROUTE_LOADING', false)
                })
              })
            },
        },
        components: {
          TimeDown
        },
        destroyed(){
          let params = {
            orderNo :this.orderNo,
            amount:this.amount,
          };
          monitoring.orderNoPay(params);//todo Facebook Pixel & Google Analytic Code    orderNoPay
        }
    }
</script>

<style scoped>
  .green{
      color: #18823a;
  }
  .blue{
      color:#23a0f7;
  }
  .Promotion-Item{
      width: 100%;
      display: flex;
      flex-direction: column;
  }
  .Promotion-Item .top{
      width: 100%;
      padding:20px 0;
      box-sizing: border-box;
      display: flex;
    align-items: center;
  }
  .Promotion-Item .top .left{
      flex: 1;
  }
  .Promotion-Item .top .left h1{
      font-size: 20px;
      font-weight: bolder;
      line-height: 25px;
      margin-bottom: 10px;
  }
  .Promotion-Item .top .left h3{
      font-size: 16px;
      line-height: 20px;
  }
  .Promotion-Item .top .right{
      /*align-self: flex-end;*/
      font-size: 15px;
    cursor: pointer;
  }
  .Promotion-Item .main{
      display: flex;
      width: 100%;
      margin-bottom: 70px;
      justify-content: space-between;
  }
  .Promotion-Item .main .left{
      width: 65%;
  }
  .Promotion-Item .main .left .paypal{
      width: 100%;
      border:1px solid #dcdcdc;
      border-radius: 1px;
  }
  .Promotion-Item .main .left .paypal h2{
      font-size: 15px;
      font-weight: bold;
      line-height: 40px;
      background: #f6f7f8;
      color:#000;
      padding:0 10px;
      box-sizing: border-box;
      border-bottom: 1px solid #dcdcdc;
  }
  .Promotion-Item .main .left .paypal .checkBox{
      display: flex;
      position: relative;
      height: 75px;
      align-items: center;
      padding:0 20px;
      box-sizing: border-box;
  }
  .Promotion-Item .main .left .paypal .checkBox span{
      width: 22px;
      height: 22px;
      margin-right: 30px;
      border:1px solid #dcdcdc;
      border-radius: 50%;
  }
  .Promotion-Item .main .left .paypal .checkBox span.checked{
      background: url(../../../static/img/00000.png) no-repeat center;
      border: none;
      background-size: 22px 22px;
  }
  .Promotion-Item .main .left .paypal .checkBox img{
      height: 25px;
  }
  .Promotion-Item .main .left .paypal .checkBox input{
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
  }
  .Promotion-Item .main .left .produectBox{
      margin-top: 15px;
      border:1px solid #dcdcdc;
      border-radius: 1px;
      width: 100%;
  }
  .Promotion-Item .main .left .produectBox h2{
      width: 100%;
      padding:0 20px;
      box-sizing: border-box;
      line-height: 40px;
      background: #f6f7f8;
      border-bottom: 1px solid #dcdcdc;
      font-size: 15px;
      font-weight: bolder;
  }
  .Promotion-Item .main .left .produectBox ul{
      width: 100%;
      overflow: hidden;
  }
  .Promotion-Item .main .left .produectBox ul li{
      display: flex;
      width: 100%;
      padding:18px 20px 0px;
      box-sizing: border-box;
      border-bottom: 1px solid #dcdcdc;
  }
  .Promotion-Item .main .left .produectBox ul li .left{
      width: 139px;
      margin-right: 20px;
      margin-bottom: 10px;
  }
  .Promotion-Item .main .left .produectBox ul li .left img{
      width: 100%;
  }
  .Promotion-Item .main .left .produectBox ul li .right{
      flex: 1;
      display: flex;
      flex-direction: column;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData{
      display: flex;
      border-bottom:1px solid #dcdcdc;
      flex: 1;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .left{
      width: 62%;
      margin:0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .left h4{
      width: 100%;
      font-size: 16px;
      line-height: 22px;
      color: #474747;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box !important;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .left h5{
      color: #98999a;
      font-size: 14px;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .left h2{
      font-size: 15px;
      background: white;
      padding: 0;
      border:none;
      line-height: 45px;
      font-weight: normal;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .left h2 span{
      font-size: 17px;
      margin-left: 10px;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .right{
      flex: 1;
      align-items: flex-end;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .right span:nth-child(1){
      font-size: 24px;
      line-height: 42px;
      display: inline-block;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .right p{
      font-size: 12px;
      margin-top: -6px;
  }
  .Promotion-Item .main .left .produectBox ul li .right .produectData .right p s{
      color:#98999a;
      font-size: 18px;
      margin-right: 5px;
  }
  .Promotion-Item .main .left .produectBox ul li .right .freeShip{
      padding-left: 27px;
      line-height: 40px;
      font-size: 14px;
      background: url(../../../static/img/promotionItemTrue.png) no-repeat left center ;
      background-size: auto 60% ;
  }
  .Promotion-Item .main .floal{
      width: 31%;
      height: 100%;
      position: relative;
  }
  .Promotion-Item .main .floal .box{
      width: 100%;
      position: absolute;
      left: 0;
      border:1px solid #dcdcdc;
      border-radius: 1px;
  }
  .Promotion-Item .main .floal .box .title{
      font-size: 19px;
      border-bottom: 1px solid #dcdcdc;
      background: #f6f7f8;
      width: 100%;
      padding-left: 15px;
      box-sizing: border-box;
      height:44px;
      line-height: 44px;
  }
  .Promotion-Item .main .floal .box .subto{
      border-bottom: 1px solid #dcdcdc;
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      padding:10px 15px;
      box-sizing: border-box;

  }
  .Promotion-Item .main .floal .box .subto div{
      display: flex;
      justify-content: space-between;
      font-size: 16px;
      color: #707070;
      line-height: 40px

  }
  .Promotion-Item .main .floal .box .subto div .green{
      color: #4999e2;
  }
  .Promotion-Item .main .floal .box .total{
      width: 100%;
      padding:20px 15px 16px;
      box-sizing: border-box;
  }
  .Promotion-Item .main .floal .box .total .price{
      display: flex;
      justify-content: space-between;
      font-size: 23px;
      font-weight: bolder;
      align-items: center;
      position:relative;
      line-height: 30px;
  }
  .Promotion-Item .main .floal .box .total .bold{
      font-weight:bold
  }
  .Promotion-Item .main .floal .box .total .saveMoney{
      position:absolute ;
      right:2px;
      bottom: -21px;
      font-size: 14px;
      line-height: 20px;
      color:#18823a;
  }
  .Promotion-Item .main .floal .box .total .price div{
      display: flex;
      flex-direction: column;
      align-items: flex-end;
  }
  .Promotion-Item .main .floal .box .total .price div .blue{
      font-size: 13px;
  }
  .Promotion-Item .main .floal .box .total h4{
      font-size: 13px;
      line-height:18px;
      margin-top: 33px;
  }
  .Promotion-Item .main .floal .box .total h4 span{
      cursor:pointer;
      color: #4999e2;
  }
  .Promotion-Item .main .floal .box .total .input{
      width: 100%;
      margin-top: 10px;
      height: 40px;
      background: #18823a;
      color: white;
      font-size: 19px;
      border-radius: 3px;
      line-height: 40px;
      text-align: center;
      position: relative;
    cursor: pointer;
  }
  .Promotion-Item .main .floal .box .total .input div{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 100;
    opacity: 0!important;
  }
  .title-left{
    float:left;
    height: 44px;
    line-height: 44px;
    font-size:18px;
  }
  .time-box{
    float:right
  }
</style>
